﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using GestorDocument.ViewModel.AsuntoTurno;
using GestorDocument.Model;
using Microsoft.Reporting.WinForms;
using System.Collections.ObjectModel;
using System.Configuration;
using MahApps.Metro.Controls;

namespace GestorDocument.UI.AsuntoTurno
{
    /// <summary>
    /// Lógica de interacción para ImprimirView.xaml
    /// </summary>
    public partial class ImprimirView : MetroWindow
    {
        public List<ReportParameter> Parametros ;
        public ImprimirView()
        {
            InitializeComponent();
        }

        private ImprimirViewModel GetViewModel()
        {
            return this.DataContext as ImprimirViewModel;
        }

        public void GetImprimir(AsuntoModel imprimirAsunto)
        {
            try
            {

                this.DataContext = new ImprimirViewModel(imprimirAsunto);
                Microsoft.Reporting.WinForms.ReportDataSource reportDataSource = new Microsoft.Reporting.WinForms.ReportDataSource();
                ImprimirViewModel viewModel = this.GetViewModel();


                if (viewModel!=null)
	            {
		            this.rvCaratula.LocalReport.ReportPath = viewModel.SuccessPathReporte;
	            }
                
                GetParametros();
                rvCaratula.LocalReport.SetParameters(Parametros);
                rvCaratula.RefreshReport();
               
            }
            catch (Exception)
            {

            }
        }

        public void GetParametros()
        {
            this.Parametros = new List<ReportParameter>();

            ImprimirViewModel viewModel = GetViewModel();
            try
            {
                if (viewModel != null)
                {
                    this.Parametros.Add(new ReportParameter("Folio", viewModel.ImprimirAsunto.Folio));
                    this.Parametros.Add(new ReportParameter("FechaRecibido", string.Format("{0:dd/MM/yyyy}", viewModel.ImprimirAsunto.FechaRecibido)));
                    this.Parametros.Add(new ReportParameter("Descripcion", viewModel.ImprimirAsunto.Descripcion));
                    this.Parametros.Add(new ReportParameter("FechaLimite", string.Format("{0:dd/MM/yyyy}", viewModel.ImprimirAsunto.FechaVencimiento)));
                    this.Parametros.Add(new ReportParameter("FechaTurnado", string.Format("{0:dd/MM/yyyy}", viewModel.ImprimirAsunto.Turno.FechaEnvio)));

                    string[] destinatarios = new string[5];
                    int cont = 0;

                    foreach (DestinatarioModel d in viewModel.ImprimirAsunto.Turno.Destinatario)
                    {

                        destinatarios[cont] = d.Rol.RolName;
                        cont = cont + 1;
                        if (5 == cont)
                            break;
                    }

                    int i = 0;

                    foreach (string v in destinatarios)
                    {
                        if (string.IsNullOrEmpty(v))
                            destinatarios[i] = " ";
                        i++;
                    }

                    this.Parametros.Add(new ReportParameter("Turnados", destinatarios));

                    cont = 0;

                    string[] doc = new string[3];

                    cont = 0;
                    foreach (DocumentosModel d in viewModel.ImprimirAsunto.Turno.Documento)
                    {
                        doc[cont] = d.DocumentoName;

                        cont = cont + 1;
                        if (3 == cont)
                        {
                            break;
                        }
                    }

                    i = 0;
                    foreach (string v in doc)
                    {
                        if (string.IsNullOrEmpty(v))
                        {
                            doc[i] = " ";
                        }
                        i++;
                    }

                    this.Parametros.Add(new ReportParameter("Anexos", doc));

                }

            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.Message);
            }
        }

    }
}
